package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * @Description NursingLevelServiceImpl
 * @Author zhangsuye
 * @Date 2024-10-18
 */
@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    /**
     * 新增护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        //创建护理计划实体类
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        // 字段填充
        try {
            // 新增入库
            nursingLevelMapper.insert(nursingLevel);
        } catch (DuplicateKeyException e) {
            throw new BaseException(BasicEnum.NAME_ALREADY_EXIST);
        }
    }



    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public NursingLevel[] getAll() {
        NursingLevel[] result = nursingLevelMapper.getAll();
        return result;
    }


    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> listByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.listByPage(name, status);

        PageResponse pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    /**
     * 根据id查询护理等级
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Long id) {
        return nursingLevelMapper.findById(id);
    }

    /**
     * 编辑护理等级
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        // 创建护理等级实体类
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        // 字段填充
        // 更新数据库
        try {
            nursingLevelMapper.update(nursingLevel);
        } catch (DuplicateKeyException e) {
            throw new BaseException(BasicEnum.NAME_ALREADY_EXIST);
        }
    }

    /**
     * 删除护理等级
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用/禁用护理等级
     * @param id
     * @param status
     */
    @Override
    public void startOrStopStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatusById(id,status);

    }
}
